package com.zxj.flights.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * http的工具类 把Http协议理解为写信的过程 通过firebug，可以查询每一个连接向服务器发出的详细信息 请求头：向服务器写信的信封
 * 响应头：服务器的回信的信封 参数：向服务器写信的内容 响应：服务器的回信的信封里面的内容
 * 
 * @author ASUS
 *
 */
public class HttpUtil
{
	/**
	 * httpget方法请求 参数==发信
	 * 
	 * @param urlStr
	 *            请求地址
	 * @param headerMap
	 *            信封的内容
	 * @param paramsMap
	 *            信的内容
	 * @return 返回url对应的网页内容;回信的内容
	 */
	public String methodGet(String urlStr, Map<String, String> headerMap, Map<String, String> paramsMap)
	{
		StringBuffer sb = new StringBuffer();
		/* get提交 */
		try
		{
			if (!urlStr.endsWith("?"))
			{
				urlStr += "?";
			}
			/* get方法提交，如果将信的内容，传递给服务器 */
			for (Iterator iterator = paramsMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry me = (Entry) iterator.next();
				urlStr += me.getKey() + "=" + me.getValue() + "&";
			}
			System.out.println(urlStr + "=====");
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			/**
			 * 如何将信封的内容设置到请求中
			 */
			for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry me = (Entry) iterator.next();
				urlConnection.setRequestProperty(me.getKey() + "", me.getValue() + "");

			}
			/* 获取输入流 */
			InputStream is = urlConnection.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = "";
			while ((line = br.readLine()) != null)
			{
				sb.append(line);
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return sb.toString();
	}

	public String methodPost(String urlStr, Map<String, String> headerMap, Map<String, String> paramsMap)
	{
		StringBuffer sb = new StringBuffer();
		try
		{
			URL url = new URL(urlStr);
			HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setDoInput(true);
			urlConnection.setDoOutput(true);
			/**
			 * 如何将信封的内容设置到请求中
			 */
			for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();)
			{
				Entry me = (Entry) iterator.next();
				urlConnection.setRequestProperty(me.getKey() + "", me.getValue() + "");
			}
			/**
			 * 向服务器传数据
			 */
			OutputStream os = urlConnection.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
			/**
			 * 如何将服务器械数据？ 通过firebug发现post请求，发送的是以下字符串
			 * returnUrl=&email=22%4022.com&password=111111ddd&code=dddd
			 */
			StringBuffer paramSb = new StringBuffer();
			for (Iterator iterator = paramsMap.keySet().iterator(); iterator.hasNext();)
			{
				String key = (String) iterator.next();
				/* 获取的是一个一个的key */
				paramSb.append(key + "=" + paramsMap.get(key) + "&");
			}
			/* 将信息发送给服务器端 */
			bw.write(paramSb.toString());
			bw.flush();
			bw.close();
			InputStream is = urlConnection.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = "";
			while ((line = br.readLine()) != null)
			{
				sb.append(line);
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		return sb.toString();
	}
}
